package chen.web.controller;

import java.util.Observable;

import chen.web.config.ConfigRepository;
import chen.web.config.SiteConfig;
import chen.web.util.ubb.*;
import chen.web.util.ubb.standard.*;

public class UbbParserFactory {

	//标准的标签工厂包含所有标签
	private static TagFactory StandardFactory = new TagFactory();
	private static TagFactory SafeFactory = new TagFactory();
	private static TagFactory SignatureFactory = new TagFactory();
	
	private static BTag b = new BTag();
	private static UTag u = new UTag();
	private static ITag i = new ITag();
	private static STag s = new STag();
	private static ImgTag img = new ImgTag();
	private static FontTag font = new FontTag();
	private static SizeTag size = new SizeTag();
	private static ColorTag color = new ColorTag();
	private static UrlTag url = new UrlTag();
	private static AlignTag align = new AlignTag();
	private static BackColorTag back = new BackColorTag();
	private static TableTag table = new TableTag();
	private static TrTag tr = new TrTag();
	private static TdTag td = new TdTag();
	private static EmotionTag emot = new EmotionTag();
	private static BanTag ban = new BanTag();
	private static HtmlTag html = new HtmlTag();
	
	
	static{
		ConfigRepository.instance().addObserver(new TagUpdate());
		url.setBlank(true);
		setEmotUrlRegex(emot);
		//注册所有标签
		StandardFactory.register(b,u,i,s,font,size,color,back,img,url,align,table,tr,td,emot,ban,html);
		//注册除html标签外的所有标签
		SafeFactory.register(b,u,i,s,font,size,color,back,img,url,align,table,tr,td,emot,ban);
		//签名需要限制标签使用
		SignatureFactory.register(b,u,i,font,size,color,url,align);
	}
	
	public static UbbParser standard(){
		EventHandler handler = new StandardEventHandler(StandardFactory);
		UbbParser parser = new UbbParser(handler);
		return parser;
	}
	
	public static UbbParser safe(){
		EventHandler handler = new StandardEventHandler(SafeFactory);
		UbbParser parser = new UbbParser(handler);
		return parser;
	}
	
	public static UbbParser forSignature(){
		EventHandler handler = new StandardEventHandler(SignatureFactory);
		UbbParser parser = new UbbParser(handler);
		return parser;
	}

	private static class TagUpdate implements java.util.Observer{

		@Override
		public void update(Observable o, Object arg) {
			// TODO Auto-generated method stub
			if(arg instanceof SiteConfig){
				setEmotUrlRegex(emot);
			}
		}
	
	}
	
	private static void setEmotUrlRegex(EmotionTag emot){
		emot.setUrlRegex(ConfigRepository.instance().get(SiteConfig.class).getBaseURL() + "/static/xheditor/xheditor_emot/?/?.gif");
	}
}
